<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="扣款订单" prop="deductionOrderCode">
        <el-input
          v-model="queryParams.deductionOrderCode"
          placeholder="请输入扣款订单"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      </el-form-item>
      <el-form-item label="客户名称" prop="consumerName">
        <el-input
          v-model="queryParams.consumerName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户电话" prop="consumerPhone">
        <el-input
          v-model="queryParams.consumerPhone"
          placeholder="请输入客户电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务单号" prop="businessNo">
        <el-input
          v-model="queryParams.businessNo"
          placeholder="请输入业务单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务状态" prop="businessStatus">
        <el-select v-model="queryParams.businessStatus" placeholder="请选择业务状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.business_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      &lt;!&ndash;          v-hasPermi="['deduction:order:add']"&ndash;&gt;-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--        >修改</el-button>-->
<!--        &lt;!&ndash;          v-hasPermi="['deduction:order:edit']"&ndash;&gt;-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--        >删除</el-button>-->
<!--        &lt;!&ndash;          v-hasPermi="['deduction:order:remove']"&ndash;&gt;-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
        <!--          v-hasPermi="['deduction:order:export']"-->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="扣款订单" align="center" prop="deductionOrderCode" />
      <el-table-column label="客户名称" align="center" prop="consumerName" />
      <el-table-column label="客户电话" align="center" prop="consumerPhone" />
      <el-table-column label="应收金额" align="center" prop="receivableAmount" />
      <el-table-column label="已收金额" align="center" prop="receivedAmount" />
      <el-table-column label="业务单号" align="center" prop="businessNo" />
      <el-table-column label="业务状态" align="center" prop="businessStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.business_status" :value="scope.row.businessStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="商户名" align="center" prop="tenantName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <!--            v-hasPermi="['deduction:order:edit']"-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--          >删除</el-button>-->
          <!--            v-hasPermi="['deduction:order:remove']"-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改扣款订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="扣款订单code" prop="deductionOrderCode">
          <el-input v-model="form.deductionOrderCode" placeholder="请输入扣款订单code" />
        </el-form-item>
        <el-form-item label="客户id" prop="consumerId">
          <el-input v-model="form.consumerId" placeholder="请输入客户id" />
        </el-form-item>
        <el-form-item label="客户名称" prop="consumerName">
          <el-input v-model="form.consumerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="客户电话" prop="consumerPhone">
          <el-input v-model="form.consumerPhone" placeholder="请输入客户电话" />
        </el-form-item>
        <el-form-item label="应收金额" prop="receivableAmount">
          <el-input v-model="form.receivableAmount" placeholder="请输入应收金额" />
        </el-form-item>
        <el-form-item label="已收金额" prop="receivedAmount">
          <el-input v-model="form.receivedAmount" placeholder="请输入已收金额" />
        </el-form-item>
        <el-form-item label="业务单号" prop="businessNo">
          <el-input v-model="form.businessNo" placeholder="请输入业务单号" />
        </el-form-item>
        <el-form-item label="业务状态状态">
          <el-radio-group v-model="form.businessStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="商户ID" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入商户ID" />
        </el-form-item>
        <el-form-item label="商户code" prop="tenantCode">
          <el-input v-model="form.tenantCode" placeholder="请输入商户code" />
        </el-form-item>
        <el-form-item label="商户名" prop="tenantName">
          <el-input v-model="form.tenantName" placeholder="请输入商户名" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/deduction/order";

export default {
  name: "Order",
  dicts: ['business_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 扣款订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deductionOrderCode: null,
        consumerId: null,
        consumerName: null,
        consumerPhone: null,
        receivableAmount: null,
        receivedAmount: null,
        businessNo: null,
        businessStatus: null,
        tenantId: null,
        tenantCode: null,
        tenantName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deductionOrderCode: [
          { required: true, message: "扣款订单code不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询扣款订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deductionOrderId: null,
        deductionOrderCode: null,
        consumerId: null,
        consumerName: null,
        consumerPhone: null,
        receivableAmount: null,
        receivedAmount: null,
        businessNo: null,
        businessStatus: "0",
        tenantId: null,
        tenantCode: null,
        tenantName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deductionOrderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加扣款订单";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      const deductionOrderId = row.deductionOrderId;
      this.$router.push("/deduction/secondDetail/record/" + deductionOrderId);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deductionOrderId = row.deductionOrderId || this.ids
      getOrder(deductionOrderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改扣款订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deductionOrderId != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deductionOrderIds = row.deductionOrderId || this.ids;
      this.$modal.confirm('是否确认删除扣款订单编号为"' + deductionOrderIds + '"的数据项？').then(function() {
        return delOrder(deductionOrderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有扣款订单数据项？').then(() => {
        this.exportLoading = true;
        return exportOrder(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
